<script setup lang="ts">
// 定义组件名
defineOptions({
  name: 'LoginPage'
})
import { loginByAPI, loginByCodeAPI } from '@/apis/user';
import router from '@/router';
import { useUserStore } from '@/stores';
import { mobileRules, passwordRules, codeRules } from '@/utils/rules';
import { showFailToast, showToast } from 'vant';

import { ref } from 'vue';

// 是否勾选我同意
const isAgree = ref(false)

const mobile = ref('')
const password = ref('')
const code = ref('')


const isPass = ref(true)


// 登录按钮
const login = async () => {
  if (!isAgree.value) return showFailToast('请勾选我已同意')
  // 验证完毕进行登录
  // console.log('111')
  // 发送请求
  // 判断是密码登录还是验证码登录
  const res = isPass.value ? await loginByAPI(mobile.value, password.value) : await loginByCodeAPI(mobile.value, code.value)
  // console.log(res)
  // 存储用户信息
  const store = useUserStore()
  store.setUser(res.data)
  // 提示用户
  showToast('登录成功')
  // 跳转页面
  router.push('/home')
}



// 切换登录方式表单校验重置
const changePass = () => {
  // 切换登陆方式
  isPass.value = !isPass.value
  // 表单校验重置
  formRef.value?.resetValidation()
}

// 发送短信验证码
import { userMobileCode } from '@/composables/useMobileCode'
// 调用组合式函数
const { onSend, time, formRef } = userMobileCode(mobile)

// 小眼睛点击显示密码
const isShow = ref(false)

</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册" @click-right="router.push('/register')"></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码' : '短信验证码' }}登录</h3>
      <a href="javascript:;" @click="changePass">
        <span>{{ isPass ? '短信验证码' : '密码' }}登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="login" ref="formRef">
      <van-field placeholder="请输入手机号" type="tel" :rules="mobileRules" v-model="mobile" name="mobile"></van-field>
      <van-field placeholder="请输入密码" :type="`${isShow ? 'text' : 'password'}`" :rules="passwordRules" v-model="password"
        v-if="isPass">
        <template #button>
          <cp-icons class="icon-eye" @click="isShow = !isShow"
            :name="`${isShow ? 'login-eye-on' : 'login-eye-off'}`"></cp-icons>
        </template>
      </van-field>
      <van-field placeholder="请输入短信验证码" :rules="codeRules" v-model="code" v-else>
        <template #button>
          <span class="btn-send" @click="onSend" :class="{ active: time > 0 }">{{ time > 0 ? `${time}秒后重新发送验证码` :
            '发送验证码' }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a class="icon"
        href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback">
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>